from common.base.base_handler import BaseHandler
from services.control.model import Control
from services.control.schemas.control_registers import ControlStates
from settings import settings


class ControlHandler(BaseHandler):
    __slots__ = ['model']

    async def handle_set_control_state(self, params: dict) -> bool:
        name, state, gain_back_brake = params['name'], params['state'], params['gain_back_brake']
        self.model.safe_write_state(name, state, gain_back_brake)

        return True

    async def handle_get_all_control_states(self) -> ControlStates:
        return self.model.control_states

    async def handle_get_control_state(self, params: dict) -> bool:
        name = params['name']
        return self.model.read_state(name)

    def __init__(self) -> None:
        self.model = Control()

        super(ControlHandler, self).__init__(request_queue_uuid=settings.CONTROL_REQUEST_QUEUE)

        self.handle('set_control_state', self.handle_set_control_state)
        self.handle('get_all_control_states', self.handle_get_all_control_states)
        self.handle('get_control_state', self.handle_get_control_state)
